/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import core.DriverManager;
import core.ScreenEngine;
import datamodel.Order;
import network.Connection;
import network.PacketBuilder;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;
import view.commands.CenterCommand;

/**
 *
 * @author Kvest
 */
public class SetOrderForm extends Form
{
    private static final int TIME_COMMAND_ID = 1;
    private static final int BACK_COMMAND_ID = 2;
    private static final int GET_COMMAND_ID = 3;
    
    private TextArea orderText;
    private Command timeCommand;
    private CenterCommand centerCommand;
    
    private ThumbList times;
    private Command backCommand;
    private Command getCommand;
    
    int defaultTitleColor;
    int defaultTitleFontColor;
    
    private Order shownOrder;
    
    public SetOrderForm()
    {
        super("setting_order");
        
        defaultTitleColor = getTitleComponent().getStyle().getBgColor();
        defaultTitleFontColor = getTitleComponent().getStyle().getFgColor();
        shownOrder = null;
        
        init();
    }
    
    private void init()
    {
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        setCyclicFocus(false);

        //Текст заказа
        orderText = new TextArea("", 1, 30);
        orderText.setEditable(false);
        orderText.setGrowByContent(true);
        
        //Команды
        timeCommand = new Command("time", TIME_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
               //Показываем выбор времени
               setTimeComponents();

               evt.consume();
            }
        };
        
        centerCommand = new CenterCommand(timeCommand);
        
        times = new ThumbList();
        times.setSelectCommandText("...");
        times.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        times.setRenderer(render);
        
        String min = (new Label("min")).getText();
        times.addItem(new CellItem(5, "5 " + min));
        times.addItem(new CellItem(10, "10 " + min));
        times.addItem(new CellItem(15, "15 " + min));
        times.addItem(new CellItem(20, "20 " + min));
        times.addItem(new CellItem(30, "30 " + min));
        times.addItem(new CellItem(45, "45 " + min));
        times.addItem(new CellItem(60, "60 " + min));
        
        backCommand = new Command("back", BACK_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
               //Показываем сам заказ
               setOrderComponents();

               evt.consume();
            }
        };
                
        getCommand = new Command("select", GET_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                CellItem selected = (CellItem)times.getSelectedItem();
                if (selected != null)
                {
                    //Берем заказ
                    getOrder(selected.getId());
                }

               evt.consume();
            }
        };
        
        times.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt) 
            {
                getCommand.actionPerformed(evt);
            }
        });
    }
    
    public void show()
    {
        setContent();
        
        super.show();
    }
    
    private void setOrderComponents()
    {
        removeAll();
        removeAllCommands();
        
        setTitle("setting_order");
        getTitleComponent().getStyle().setBgColor(0xffff00);
        getTitleComponent().getStyle().setFgColor(0);
        
        addComponent(orderText);
        
        if (!Display.getInstance().isTouchScreenDevice())
        {
            addCommand(centerCommand);
        }    
        addCommand(timeCommand);

        repaint();
    }
    
    private void setTimeComponents()
    {
        removeAll();
        removeAllCommands();
        
        setTitle("time");
        getTitleComponent().getStyle().setBgColor(defaultTitleColor);
        getTitleComponent().getStyle().setFgColor(defaultTitleFontColor);
        
        addComponent(times);
        setFocused(times);
        
        addCommand(getCommand);
        addCommand(backCommand);

        repaint();
    }
    
    private void setContent()
    {        
        if (shownOrder != null)
        {
            orderText.setText(shownOrder.getFullText());
        }
        else
        {
            orderText.setText("");
        }

        setOrderComponents();
    }

    private void getOrder(int time)
    { 
        if (shownOrder != null)
        {
            //Удаляем форму заказа
            ScreenEngine.getEngine().pop(false);

            //Отображаем форму прогресса
            ScreenEngine.getEngine().push(new ProgressForm());

            //Формируем запрос
            byte[] data = PacketBuilder.createGetOrderRequest(DriverManager.getManager().getDriverID(), shownOrder.getId(), time);

            //Отправляем запрос
            boolean sendSuccess = Connection.getConnection().send(data);
            
            shownOrder = null;
        }
            
        //Возвращаемся на предыдущую форму
        ScreenEngine.getEngine().pop(true);
    }
    
    public void setShownOrder(Order shownOrder) 
    {
        this.shownOrder = shownOrder;
    }
}
